import { Show,plugin ,pic ,findIndexByName,Strand,getNonZeroKeys,startstatus,stopstatus,gettupo,getstring,checkZeroValue,checkAllZeroValues,
    checkNameExists,player_zhanli,Add_bag_thing, player_zhandou,determineWinner,getB_qq,createPlayerObject } from "../../api.js";
import { create_player,existplayer,Read_player,Write_player,武者境界, 灵魂境界,体魄境界,user_id,finduid,兑换码} from '../../model/gameapi.js';
  export class exchange extends plugin  {
      constructor() {
          super({
              /** 功能名称 */
              name: 'dajie',
              /** 功能描述 */
              dsc: '基础模块',
              event: 'message',
              /** 优先级，数字越小等级越高 */
              priority: 600,
              rule: [
                  {
                      reg: /^(#|\/)兑换码.*$/,
                      fnc: 'exchange',
                    },
              ],
          });
      }
    async exchange(e){
        const usr_qq = e.user_id;
        const [thing_name] = e.msg.replace(/(\/|#)?兑换码/, "").trim().split("*").map(code => code.trim());
        if (!await existplayer(1, usr_qq) || !thing_name) return false;
        let results = await Read_player(1,usr_qq)
        let player = results.player;
        let bag = results.bag;
        let exchange_list= await 兑换码.findAll({raw:true});
        let exchange_thing = exchange_list.find(item => item.name == thing_name);
        // 将日期时间字符串转换为 Date 对象
        const startTime = new Date(exchange_thing.start_time);
        const endTime = new Date(exchange_thing.end_time);
        // 获取毫秒数
        const startTimeInMillis = startTime.getTime();
        const endTimeInMillis = endTime.getTime();
        //当前时间毫秒数
        const currentMilliseconds = new Date().getTime();
        if(currentMilliseconds < startTimeInMillis) return e.reply(`兑换码${thing_name}还没有开放,开放时间为【${exchange_thing.start_time}】`);
        if(currentMilliseconds > endTimeInMillis) return e.reply(`兑换码${thing_name}已过期,截至时间为【${exchange_thing.end_time}】`);
    }
    
    }